package com.yxb.teaching_assessment.biz.controller;

import com.yxb.teaching_assessment.biz.entity.Student;
import com.yxb.teaching_assessment.biz.entity.Teacher;
import com.yxb.teaching_assessment.biz.entity.User;
import com.yxb.teaching_assessment.biz.service.StudentService;
import com.yxb.teaching_assessment.biz.service.TeacherService;
import com.yxb.teaching_assessment.biz.service.UserService;
import com.yxb.teaching_assessment.biz.vo.AbstractBaseCtrl;
import com.yxb.teaching_assessment.biz.vo.ResultVO;
import com.yxb.teaching_assessment.common.jwt.JWT;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "用户模块接口")
@RequestMapping("/user")
public class UserController extends AbstractBaseCtrl {
    @Autowired
    UserService userService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    StudentService studentService;

    /**
     * 登录接口
     * @param user
     * @return
     */
    /*@PostMapping("/login")
    @ResponseBody
    @ApiOperation(value = "用户登录")
    public JSONObject login(@RequestBody User user) {
        JSONObject ret=new JSONObject();
        User userForBase=userService.findUserByUsername(user);
        if(userForBase==null){
            ret.put("result","0");
            ret.put("message","用户名错误");
            ret.put("data","error");
            return ret;
        }else {
            if (!userForBase.getPassword().equals(user.getPassword())){
                ret.put("result","0");
                ret.put("message","密码错误");
                ret.put("data","error");
                return ret;
            }else {
                String token = tokenService.getToken(userForBase);
                ret.put("token", token);
                ret.put("message","登录成功");
                return ret;
            }
        }
    }*/

    /**
     * 登录接口
     */

    @PostMapping("/login")
    @ApiOperation(value = "登录接口")
    public ResultVO login(@RequestBody User user) {
        User user1=userService.findUserByUsername(user);
        if (user1.getUsername().equals(user.getUsername()) && user1.getPassword().equals(user.getPassword())) {
            /* responseData.putDataValue("user", user);*/
            String token = JWT.sign(user1, 30L * 24L * 3600L * 1000L);//一个月的时间
            if (token != null) {
                return success(token);
            }
        }
        return error("用户名或密码错误");
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户激活（注册）")
    public ResultVO register(@RequestParam("username")String username){
        Student student = studentService.findByStudentNo(username);
        if (student!=null){
            User user = new User();
            user.setUsername(username);
            user.setPassword("123456");
            user.setStudentid(student.getStudentid());
            int ret = userService.insertUser(user);
            if (ret==1){
                return success("学生注册成功");
            }else {
                return success("学生注册失败");
            }
        }else {
            Teacher teacher = teacherService.findByTeacherno(username);
            if (teacher!=null){
                User user = new User();
                user.setUsername(username);
                user.setPassword("123456");
                user.setTeacherid(teacher.getTeacherid());
                int ret = userService.insertUser(user);
                if (ret==1){
                    return success("教师注册成功");
                }else {
                    return success("教师注册失败");
                }
            }else {
                return success("该账号不存在");
            }
        }
    }

}
